package com.zfzs.post.modules.post.service.impl;

import com.alibaba.fastjson.JSON;
import com.zfzs.post.model.vo.QueryAddAgentListVO;
import com.zfzs.post.model.vo.TerminalTypeVO;
import com.zfzs.post.modules.backstage.domain.UserInfo;
import com.zfzs.post.modules.backstage.service.BackStageUserService;
import com.zfzs.post.modules.post.domain.TerminalType;
import com.zfzs.post.modules.post.repository.TerminalTypeRepository;
import com.zfzs.post.modules.post.service.TerminalTypeService;
import com.zfzs.post.modules.post.service.dto.TerminalTypeQueryCriteria;
import com.zfzs.post.modules.post.service.mapper.TerminalTypeMapper;
import com.zfzs.post.utils.PageUtil;
import com.zfzs.post.utils.QueryHelp;
import com.zfzs.post.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

/**
 * 终端类型
 *
 * @author ck
 * @date 2019-09-27
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class TerminalTypeServiceImpl implements TerminalTypeService {

    @Autowired
    private TerminalTypeRepository postTerminalTypeRepository;

    @Autowired
    private TerminalTypeMapper postTerminalTypeMapper;

    @Autowired
    private BackStageUserService backStageUserService;


    @Override
    public Map<String, Object> queryAll(TerminalTypeQueryCriteria criteria, Pageable pageable) {
        Map<String, Object> map = new HashMap<>();
        //获取渠道ID
        Long id = SecurityUtils.getUserId();
        //管理员数据
        if (!id.equals(1L)) {
            criteria.setChannelId(id);
        }
        Page<TerminalType> page = postTerminalTypeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        List<TerminalType> list = page.getContent();
        List<TerminalTypeVO> collect = list.stream()
                .map(e -> {
                    TerminalTypeVO vo = JSON.parseObject(JSON.toJSONString(e), TerminalTypeVO.class);
                    Long channelId = vo.getChannelId();
                    UserInfo user = backStageUserService.findById(channelId);
                    if (user != null) {
                        vo.setChannelName(user.getNickname());
                    }
                    //查询图片
                    String imgUrl = postTerminalTypeRepository.findUrlByImgId(e.getDescImgId());
                    vo.setDescImgUrl(imgUrl);
                    return vo;
                }).collect(Collectors.toList());
        map.put("totalElements", page.getTotalElements());
        map.put("content", collect);
        return map;
    }

    @Override
    public Object queryAll(TerminalTypeQueryCriteria criteria) {
        return postTerminalTypeMapper.toDto(postTerminalTypeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder)));
    }

    @Override
    public TerminalType findById(Long id) {
        Optional<TerminalType> postTerminalType = postTerminalTypeRepository.findById(id);
        return postTerminalType.get();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TerminalType create(TerminalType resources) {
        resources.setChannelId(SecurityUtils.getUserId());
        resources.setCreateTime(new Timestamp(System.currentTimeMillis()));
        return postTerminalTypeRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(TerminalType resources) {
        Optional<TerminalType> optionalPostTerminalType = postTerminalTypeRepository.findById(resources.getId());
        TerminalType postTerminalType = optionalPostTerminalType.get();
        resources.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        postTerminalType.copy(resources);
        postTerminalTypeRepository.save(postTerminalType);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        postTerminalTypeRepository.deleteById(id);
    }

    @Override
    public List<TerminalType> findByChannelId(Long channelId) {
        return postTerminalTypeRepository.findByChannelId(channelId);
    }
}